package andy.demo.webView;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import andy.demo.R;
import andy.demo.utils.IOUtil;

public class WebViewVOA  extends Activity{
	
	private static final String TAG = WebViewVOA.class.getSimpleName();

	WebView voa;
	
	String voa_title = "http://www.51voa.com/VOA_Standard_English/war-of--flag-still-inspires-after--years-46794.html";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 在标题栏上显示进度  
		getWindow().requestFeature(Window.FEATURE_PROGRESS);  
		
		setContentView(R.layout.webview_voa);
		
		
        
        voa = (WebView)this.findViewById(R.id.wv_voa);
        
        String mimeType = "text/html";
        String encoding = "utf-8";
        
     // 滚动条风格  
        voa.setScrollBarStyle(0);
        // 设置JS可用  
        voa.getSettings().setJavaScriptEnabled(true);
        
        //设置可以支持缩放   
        voa.getSettings().setSupportZoom(true);   
        //设置默认缩放方式尺寸是far   
//        wv.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);  
        //设置出现缩放工具   
        voa.getSettings().setBuiltInZoomControls(true);
        
        
		
	     /* 
         * WebChromeClient类:用来辅助WebView处理JavaScript的对话框,网站图标,网站Title,加载进度等 
         * 通过setWebChromeClient调协WebChromeClient类 
         */  
        voa.setWebChromeClient(new WebChromeClient() {  
            // 加载进度中,100时停止  
            public void onProgressChanged(WebView view, int progress) {  
                WebViewVOA.this.setProgress(progress * 100);  
            }  
  
            @Override  
            public void onReceivedTitle(WebView view, String title) {  
            	WebViewVOA.this.setTitle(title);  
            }  
  
        });  
  
        /* 
         * WebViewClient类: 用来辅助WebView处理各种通知,请求等事件的类 
         * 通过setWebViewClient设置WebViewClient类 
         */  
        voa.setWebViewClient(new WebViewClient() {  
            // 页面加载失败  
            public void onReceivedError(WebView view, int errorCode,  
                    String description, String failingUrl) {  
                Toast.makeText(WebViewVOA.this, "异常:! " + description,  
                        Toast.LENGTH_LONG).show();  
            }  
  
        });  
        
        String data = getHTMLbyFile();
       
//        voa.loadUrl(voa_title);
        
//        voa.loadUrl("file:///android_asset/index.html");
//        voa.loadData(getHTMLbyFile(), mimeType, "utf-8");
        
        //1.5时的处理过程
//        try {
//			voa.loadData(URLEncoder.encode(getHTMLbyFile(), "utf-8"), mimeType, "utf-8");
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
        
        
        voa.loadDataWithBaseURL(null, data, mimeType, encoding, null);
  
    }  
  
    private String getHTMLbyFile() {
    	String result = "";
		try {
			InputStream is = this.getAssets().open("index.html", AssetManager.ACCESS_STREAMING);
			result = IOUtil.inputStream2String(is, "utf-8");
			is.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		Log.d(TAG, "result =" + result);
		return result;
	}

	/* 
     * 通过WebView的goBack(),goForward()方法设置其前进和后退 
     */  
    @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event) {  
        // TODO Auto-generated method stub  
        if (keyCode == KeyEvent.KEYCODE_BACK && voa.canGoBack()) {  
            // 返回前一个页面  
            voa.goBack();  
            return true;  
        }  
        return super.onKeyDown(keyCode, event);  
    }  
		
}
